Ext.ux.DateField = Ext.extend(Ext.form.DateField, {
         allowBlank:true
        ,altFormats :DATE_FIELD_ALT_FORMAT
        ,anchor:'95%'
});
Ext.reg('ux.datefield', Ext.form.DateField);



Ext.ux.TextArea = Ext.extend(Ext.form.TextArea, {
         msgTarget: 'under'
        ,allowBlank:true
        ,grow : true
        ,anchor:'95%'
});
Ext.reg('ux.textarea', Ext.ux.TextArea);



Ext.ux.TextField = Ext.extend(Ext.form.TextField, {
         msgTarget: 'under'
        ,allowBlank:true
        ,anchor:'95%'
});
Ext.reg('ux.textfield', Ext.ux.TextField);




Ext.ux.FormPanel = Ext.extend(Ext.FormPanel, {
         labelAlign: 'right'
        ,autoScroll:true        
        ,bodyStyle:'padding:5px 5px 0'
//        ,defaultType: 'ux.textfield'//如果定义了这个，那么如果去掉xtype:'fieldset'就会使得页面无法显示
        ,width: 700
});




Ext.ux.GroupingStore = function(config){
    Ext.apply(this,config);
    this.proxy = new Ext.data.HttpProxy({
           url: this.url
    });
    this.reader = new Ext.data.JsonReader(
          {
            root: 'results'
           ,totalProperty: 'totalCount'
           ,id: this.storeIdAndSortField
          }
         ,this.record
    );
    this.sortInfo = {field: this.storeIdAndSortField, direction: 'ASC'};
    this.remoteSort = true;
    Ext.ux.GroupingStore.superclass.constructor.call(this); 
};
Ext.extend(Ext.ux.GroupingStore, Ext.data.GroupingStore, {});




Ext.ux.Store = function(c){
    Ext.apply(this,c);
    Ext.ux.Store.superclass.constructor.call(this, Ext.apply(c, {
        proxy: new Ext.data.HttpProxy({url:this.url, method:'GET'}),
        reader: new Ext.data.JsonReader(
          {
            root: 'results'
           ,totalProperty: 'totalCount'
           ,id: this.storeIdAndSortField
          }
         ,this.record
        ),
        sortInfo: {field: this.storeIdAndSortField, direction: 'ASC'},
        remoteSort: true
    }));
};
Ext.extend(Ext.ux.Store, Ext.data.Store);




Ext.ux.FilterStore = function(c){
    Ext.apply(this,c);
    Ext.ux.Store.superclass.constructor.call(this, Ext.apply(c, {
        proxy: new Ext.data.HttpProxy({
           url: '/MISFT/CodeJson.do'
        })
       ,reader: new Ext.data.JsonReader(
          {
            root: 'results'
           ,totalProperty: 'totalCount'
           ,id: 'id'
          }
         ,['id','text']
        )
       ,baseParams:{codeJson:"{codeObjectName:'"+this.codeObjectName+"'}"}
       ,sortInfo: {field: 'id', direction: 'ASC'}
       ,remoteSort: true
    }));
};
Ext.extend(Ext.ux.FilterStore, Ext.data.Store);

    


Ext.ux.CodeComboBox = function(config){
    Ext.ux.CodeComboBox.superclass.constructor.call(this, config);
    Ext.apply(this,config);    
    this.valueField = 'id';
    this.displayField = 'text';
    this.store = new Ext.ux.Store({
           url: '/MISFT/CodeJson.do'
          ,record:[this.valueField,this.displayField]//record的简写形式
          //,storeIdAndSortField: 'id'
    }); 
    this.loaded = false;
    if(this.parentComboBox&&this.parentField){  
    //如果指定了父下拉框，那么要在父与子之间建立级联关系                 
//          if(this.getValue()){
//             this.params = {codeJson:"{'codeObjectName':'"+this.codeObjectName+"'}"};
//             this.store.load({params:this.params});
//          }
          this.parentComboBox.on("select",function(){
                                               this.clearValue();
                                               var codeValue = (this.parentComboBox.getValue()!=="" ? this.parentComboBox.getValue() : null);
                                               if(codeValue!==null){
                                                   this.params = {codeJson:"{'codeObjectName':'"+this.codeObjectName+"','codeName':'"+this.parentField+"','codeValue':"+codeValue+"}"};
                                                   //alert("1");
                                                   this.store.load({params:this.params});
                                                   this.loaded = true;
                                               }
                                     },this);
          //this.on("valid",function(){alert("2");this.reload();this.loaded = true;},this,{single: true});
    }else{
          this.params = {codeJson:"{'codeObjectName':'"+this.codeObjectName+"'}"};
          //
          this.on("focus",function(){
                             if(this.getValue()==0||this.getValue()==null||this.getValue()==''){
                                if(!this.loaded){
                                  //alert("focus load");
                                  //alert("3");
                                  this.store.load({params:this.params});
                                  this.loaded = true
                                }
                             }
                          },this);
//          this.on("beforequery",function(){alert("beforequery");},this,{single: true});
//          this.on("expand",function(){alert("expand");},this,{single: true});
//          this.on("focus",function(){alert("focus");},this,{single: true});
//          this.on("select",function(){alert("select");},this,{single: true});

          this.on("valid",function(){
                             //alert("valid");
                             if(this.getValue()!==0&&this.getValue()!==null&&this.getValue()!==''){
                                if(!this.loaded){
                                  //alert("valid load");
                                  //alert("4");
                                  this.load();
                                  this.loaded = true
                                }
                             }
                          },this);
    }
};
Ext.extend(Ext.ux.CodeComboBox, Ext.form.ComboBox, {  
     typeAhead: true
    ,mode: 'local'
    ,resizable: true
    //,valueNotFoundText : '没有对应的信息！' 
    ,triggerAction: 'all'
    ,emptyText:'请选择！'
    ,selectOnFocus:true
    ,lazyInit : true
    
    ,reload : function(){ 
        if(this.parentComboBox&&this.parentField){
            var codeValue = (this.parentComboBox.getValue()!=="" ? this.parentComboBox.getValue() : null);
            if(this.getValue()){
               this.params = {codeJson:"{'codeObjectName':'"+this.codeObjectName+"','codeName':'"+this.parentField+"','codeValue':"+codeValue+"}"};
               this.store.on("load",function(){this.setValue(this.getValue())},this,{single: true});
               //alert("5");
               this.store.load({params:this.params});               
            }
        }else{
            //alert("6");
            this.store.reload();
        }       
    }
    
    ,load : function(){
        this.store.on("load",function(){this.setValue(this.getValue())},this,{single: true});
        this.store.load({params:this.params});
    }
    
});
Ext.reg('codecombo', Ext.ux.CodeComboBox);













Ext.ux.SearchGrid = function(config, container){
        Ext.apply(this,config); 
//父面板的id，比如父面板是ForeignerGrid，子面板是MovieGrid，
//子面板显示ForeignerGrid中id为111的外国人的ForeignerAwardExperience信息，
//那么parentSelectedId就为111，这个字段用于判断是否需要重新加载子面板的数据
        this.parentSelectedId = null;
        this.store = new Ext.ux.Store({//注意不能搞成this.ds了
           url: this.url
          ,record: this.record
          ,storeIdAndSortField: this.storeIdAndSortField
        });        
        this.cm.defaultSortable = true;
        this.per_page_combo_box = new Ext.form.ComboBox({
            store: PER_PAGE_STORE,
            displayField:'txt',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            selectOnFocus:true,
            resizable:true,
            listWidth: 50,
            width: 50,
            value: '10'
        });
        this.per_page_combo_box.on("select",function(){
                   var PerPage = parseInt(this.per_page_combo_box.getValue());
                   this.setPerPage(PerPage);},this
        );     
        this.paging = this.bbar = new Ext.PagingToolbar({
            pageSize: PAGE_SIZE
           ,store: this.store
           ,displayInfo: true
           ,items: ['-',NUMBER_PER_PAGE_LABEL, this.per_page_combo_box]
        });    
        this.tbar = [ {
			pressed : false,
			enableToggle : false,
			icon : SEARCH_ICON,
			text : SEARCH_BOTTON_LABEL,
			cls : 'x-btn-text-icon',
			handler : this.doSearch,
			scope : this
		}, '-', {
			pressed : false,
			enableToggle : false,
			icon : ADD_ICON,
			text : ADD_BOTTON_LABEL,
			cls : 'x-btn-text-icon',
			handler : this.doAdd,
			scope : this
		}, '-', {
			pressed : false,
			enableToggle : false,
			icon : UPDATE_ICON,
			text : UPDATE_BOTTON_LABEL,
			cls : 'x-btn-text-icon',
			handler : this.doUpdate,
			scope : this
		}, '-', {
			pressed : false,
			enableToggle : false,
			icon : DEL_ICON,
			text : DEL_BOTTON_LABEL,
			cls : 'x-btn-text-icon',
			handler : this.doDel,
			scope : this
		}, new Ext.Toolbar.Fill, {
			pressed : false,
			enableToggle : false,
			icon : EXPORT_ICON,
			text : EXPORT_BOTTON_LABEL,
			cls : 'x-btn-text-icon',
			scope : this
		}];
    this.updateFormPanel = new Ext.ux.FormPanel({
         url:this.url+'?action=put'////////////////////////////////////////////////////////////////////////////
        ,items: this.updateItems
    });
    
    this.addFormPanel = new Ext.ux.FormPanel({
         url:this.url+'?action=post'////////////////////////////////////////////////////////////////////////////
        ,items: this.addItems
    });
    this.searchFormPanel = new Ext.ux.FormPanel({
         url:this.url+'?action=search'////////////////////////////////////////////////////////////////////////////
        ,items: this.searchItems
    });
   
    Ext.ux.SearchGrid.superclass.constructor.call(this, container, config); 
};

Ext.extend(Ext.ux.SearchGrid, Ext.grid.EditorGridPanel, {
     width: 700
    ,height: 326
 	//,frame:true//是否用一个框框住
    ,enableColLock:true
    ,collapsible: true
    ,animCollapse: false
    ,trackMouseOver:true
//    ,sm: this.sm
    ,loadMask: true
    ,modelName:''
    //,view: new Ext.grid.GroupingView()//如果使用分组显示插件，如果在一个页面有两个表格，会使得在前面加载的表格无法显示加载的数据
    ,viewConfig: {
        forceFit:true,// prevent horizontal scrolling
        autoFill:true //True to auto expand the columns to fit the grid when the grid is created.
     }
    
    ,initComponent : function(){
        Ext.ux.SearchGrid.superclass.initComponent.call(this);
        this.addWinRendered = false;
        this.searchWinRendered = false;
        this.viewWinRendered = false;
        this.updateWinRendered = false;
        this.on("rowdblclick",function(){
             if(this.updateWin){   
                if(!this.updateWin.isVisible()){
                  this.doUpdate();
                }             
             }else{
               this.doUpdate();
             }
        },this); 
    }
	
	,updateDB : function(oGrid_event){	
	        var json = "{'field': '"+oGrid_event.field+"','id':"+oGrid_event.record.id+",'value':"+oGrid_event.value+"}";
	        Ext.Ajax.request({
                url:this.url+'?action=updateField', ////////////////////////////////////////////////////////////////////////////////////////////////
                method: 'POST',
                params: {json:json},
                callback: function(options, success, response) {
                }
            });     
    }
	
	,setPerPage : function(pageSize){
	        var store = this.store;
	        var paging = this.paging;
	        paging.pageSize = pageSize;
	    	paging.cursor = 0;
                ps = paging.pageSize;
	        store.baseParams = {limit:ps };
                store.reload({params:{start:0}})
    } 
    
    ,getSelectionId : function(){
	    var m = this.getSelections();
        if(m.length > 0){return m[0].id;}
	}
    
    ,doView : function(grid, rowIndex, e) {    
        var m = this.getSelections();
        if(m.length > 0){
            var paging = this.paging;
            var viewFormPanel = this.viewFormPanel;
            var viewForm = viewFormPanel.getForm();
		    var selectedId = m[0].id;
	     	var store = this.store;
	     	var data = store.getById(selectedId).json;	     	
 	        if(!this.viewWinRendered){ 
              viewWin = new Ext.Window({
                layout:'fit',
                collapsible:true,
                modal:true,//make the window modal and mask everything behind it when displayed
                minimizable:true,
                maximizable:true,
                constrainHeader:true,
                title:PLEASE_VIEW_TITLE + this.modelName,
                width:this.winWidth,
                height:this.winHeight,
                closeAction:'hide',
                autoScroll:true,
                plain: true,
                autoDestroy : true,
                items: viewFormPanel,                
                buttons: [{
                    text: DEL_BOTTON_LABEL,
                    icon: DEL_ICON,
                    cls:"x-btn-text-icon",
                    handler: function(){
                         Ext.MessageBox.confirm(DELETE_CONFIRM_MESSAGE_TITLE_LABEL, DELETE_CONFIRM_MESSAGE_LABEL  
                         ,function(btn){                	    
                           if(btn == 'yes'){		
	                   	     var jsonData = "[{\"id\":\"" + selectedId + "\"}]";
		        		     store.remove(m[0]); 
			                 store.load({params:{start:paging.cursor, limit:PAGE_SIZE, action:"delete", delIds:jsonData}});
			                 viewFormPanel.getForm().reset();
			                 viewWin.hide();
                          }
                        })
                      }
                },{
                    text: CLOSE_BOTTON_LABEL,
                    icon: CLOSE_ICON,
                    cls:"x-btn-text-icon",
                    handler: function(){
                        viewWin.hide();
                    }
                    ,scope: this 
                }]
              });
            }
	        this.viewWinRendered = true;
            viewWin.show();
            viewForm.setValues(data);
	     }else{Ext.MessageBox.alert(VIEW_CONFIRM_ERROR_TITLE_LABEL, VIEW_CONFIRM_ERROR_LABEL);}
 	}
 	
 	,doSearch : function(){ 	
 	    var paging = this.paging;
 	    var searchFormPanel = this.searchFormPanel;
 	    var searchForm = searchFormPanel.getForm();
 	    var store = this.store; 	    
 	    if(!this.searchWinRendered){ 
        var searchWin = this.searchWin = new Ext.Window({
                layout:'fit',
                collapsible:true,
                modal:true,//make the window modal and mask everything behind it when displayed
                minimizable:true,
                maximizable:true,
                constrainHeader:true,
                title:PLEASE_SEARCH_TITLE + this.modelName,
                width:this.winWidth,
                height:this.winHeight,
                closeAction:'hide',
                plain: true,
                autoDestroy : true,
                items: searchFormPanel,
                buttons: [{
                     text: SAVE_BOTTON_LABEL
                    ,icon: SAVE_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        if (searchForm.isValid()) {
				           	var formJsonString = Ext.util.JSON.encode(searchForm.getValues(false));				           	
				               searchForm.submit({
					           	waitMsg: SEARCH_WAIT_MESSAGE_LABEL,
					           	reset: false,
				           		params:{formJsonString : formJsonString},
					           	failure: function(form, action) {
					           	    Ext.MessageBox.alert(SEARCH_ERROR_MESSAGE_TITLE_LABEL, action.result.errorInfo);
					           	},
					           	success: function(form, action) {
						               Ext.Msg.alert('Click', 'You did something.');
						               searchWin.hide();
						               store.load({params:{start:paging.cursor, limit:PAGE_SIZE}});
					           	}
				           	});					
			           	}else{
					           Ext.MessageBox.alert(FORM_NOT_VALID_MESSAGE_TITLE_LABEL, FORM_NOT_VALID_MESSAGE_LABEL);
				        }
                    }
                },{
                    text: RESET_BOTTON_LABEL
                    ,icon: RESET_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        searchForm.reset();
                    }
                    ,scope: this 
                },{
                    text: CLOSE_BOTTON_LABEL
                    ,icon: CLOSE_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        this.searchWin.hide();
                    }
                    ,scope: this 
                }]
            });
       }       
	   this.searchWinRendered = true;
       this.searchWin.show();
	}
 	 	
 	,doAdd : function(){ 	
 	    var paging = this.paging;
 	    var addFormPanel = this.addFormPanel;
 	    var addForm = addFormPanel.getForm();
 	    var store = this.store; 	    
 	    if(!this.addWinRendered){ 
        var addWin = this.addWin = new Ext.Window({
                layout:'fit',
                collapsible:true,
                modal:true,//make the window modal and mask everything behind it when displayed
                resizeHandles:'all',
                minimizable:true,
                maximizable:true,
                constrainHeader:true,
                title:PLEASE_ADD_TITLE + this.modelName,
                width:this.winWidth,
                height:this.winHeight,
                closeAction:'hide',
                plain: true,
                autoDestroy : true,
                items: addFormPanel,
                buttons: [{
                     text: SAVE_BOTTON_LABEL
                    ,icon: SAVE_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        if (addForm.isValid()) {
				           	var formJsonString = Ext.util.JSON.encode(addForm.getValues(false));				           	
				               addForm.submit({
					           	waitMsg:ADD_WAIT_MESSAGE_LABEL,
					           	reset: false,
				           		params:{formJsonString : formJsonString},
					           	failure: function(form, action) {
					           	    Ext.MessageBox.alert(ADD_ERROR_MESSAGE_TITLE_LABEL, action.result.errorInfo);
					           	},
					           	success: function(form, action) {
						               Ext.Msg.alert('Click', 'You did something.');
						               addWin.hide();
						               store.load({params:{start:paging.cursor, limit:PAGE_SIZE}});
					           	}
				           	});					
			           	}else{
					           Ext.MessageBox.alert(FORM_NOT_VALID_MESSAGE_TITLE_LABEL, FORM_NOT_VALID_MESSAGE_LABEL);
				        }
                    }
                },{
                    text: RESET_BOTTON_LABEL
                    ,icon: RESET_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        addForm.reset();
                    }
                    ,scope: this 
                },{
                    text: CLOSE_BOTTON_LABEL
                    ,icon: CLOSE_ICON
                    ,cls:"x-btn-text-icon"
                    ,handler: function(){
                        this.addWin.hide();
                    }
                    ,scope: this 
                }]
            });
       }       
	   this.addWinRendered = true;
       this.addWin.show();
	}	
	    
    ,doUpdate : function(grid, rowIndex, e) {    
        var m = this.getSelections()
        if(m.length > 0){
            var selectedId = m[0].id;
            var paging = this.paging;
            var updateFormPanel = this.updateFormPanel;
            var updateForm = updateFormPanel.getForm();		
	     	var store = this.store;	
	     	var data = store.getById(selectedId).json;
	     	if(!this.updateWinRendered){ 
            var updateWin = this.updateWin = new Ext.Window({
                layout:'fit',
                collapsible:true,
                //modal:true,//make the window modal and mask everything behind it when displayed
                minimizable:true,
                maximizable:true,
                constrainHeader:true,
                title:PLEASE_UPDATE_TITLE + this.modelName,
                width:this.winWidth,
                height:this.winHeight,
                closeAction:'hide',
                plain: true,
                autoDestroy : true,                
                items: updateFormPanel,                
                buttons: [{
                    text: SAVE_BOTTON_LABEL,
                    icon: SAVE_ICON,
                    cls:"x-btn-text-icon",
                    handler: function(){
                   	  if (updateForm.isValid()) {
					 	 var formJsonString = Ext.util.JSON.encode(updateForm.getValues(false));
						    updateForm.submit({
								waitMsg:UPDATE_WAIT_MESSAGE_LABEL,
//								reset: false,
								params:{formJsonString : formJsonString},
								failure: function(form, action) {
								    Ext.MessageBox.alert(UPDATE_ERROR_MESSAGE_TITLE_LABEL, action.result.errorInfo);
								},
								success: function(form, action) {
								    Ext.MessageBox.alert(UPDATE_SUCCESS_CONFIRM_MESSAGE_TITLE_LABEL, action.result.info);
								    updateWin.hide();
								    store.load({params:{start:paging.cursor, limit:PAGE_SIZE}});
								}
							});					
						}else{
							Ext.MessageBox.alert(FORM_NOT_VALID_MESSAGE_TITLE_LABEL, FORM_NOT_VALID_MESSAGE_LABEL);
						}
                    }
                    ,scope: this
                },{
                    text: RESET_BOTTON_LABEL,
                    icon: RESET_ICON,
                    cls:"x-btn-text-icon",
                    handler: function(){
                        updateForm.reset();
                    }
                    ,scope: this 
                },{
                    text: CLOSE_BOTTON_LABEL,
                    icon: CLOSE_ICON,
                    cls:"x-btn-text-icon",
                    handler: function(){
                        this.updateWin.hide();
                    }
                    ,scope: this 
                }]
            });
            updateWin.on('minimize', function(){
                 //updateWin.toggleCollapse(false);
                 updateWin.hide(false);
            });
            
       }
       this.updateWin.show();
       if(this.updateWinRendered == true){updateForm.reset();}//如果不重置，那么即使当前要查看的数据中日期框中没有数据，上一次日期框中的数据也不会消失
       updateForm.setValues(data);
       this.updateWinRendered = true;
	 }else{Ext.MessageBox.alert(UPDATE_CONFIRM_ERROR_TITLE_LABEL, UPDATE_CONFIRM_ERROR_LABEL);}
 	}

    ,doDel : function(){
        var m = this.getSelections();    
        if(m.length > 0){
            var paging = this.paging;
            var store = this.store;
        	Ext.MessageBox.confirm(DELETE_CONFIRM_MESSAGE_TITLE_LABEL, DELETE_CONFIRM_MESSAGE_LABEL  
                ,function(btn){                	    
                  if(btn == 'yes'){		
	        	    var jsonData = "[";
	                for(var i = 0, len = m.length; i < len; i++){        		
		        		var ss = "{\"id\":\"" + m[i].id + "\"}";
		        		if(i==0)
	                   		jsonData = jsonData + ss ;
		        	   	else
			        		jsonData = jsonData + "," + ss;	
		        		store.remove(m[i]);								
	                }	
		            jsonData = jsonData + "]";			
			        store.load({params:{start:paging.cursor, limit:PAGE_SIZE, action:"delete", delIds:jsonData}});
                  }
        	   }
        	);	
        }
        else
        {
        	Ext.MessageBox.alert(DELETE_CONFIRM_ERROR_TITLE_LABEL, DELETE_CONFIRM_ERROR_LABEL);
        }     
    }
    
    ,setContextMenu : function(){
        if(this.viewFormPanel)
              this.contextMenu.add(new Ext.menu.Item({icon: VIEW_ICON, text: VIEW_BOTTON_LABEL, handler: this.doView, scope: this}));
        if(this.addFormPanel)
              this.contextMenu.add(new Ext.menu.Item({icon: ADD_ICON, text: ADD_BOTTON_LABEL, handler: this.doAdd, scope: this}));
        if(this.updateFormPanel)
            this.contextMenu.add(new Ext.menu.Item({icon: UPDATE_ICON, text: UPDATE_BOTTON_LABEL, handler: this.doUpdate, scope: this}));
        this.contextMenu.add(new Ext.menu.Item({icon: DEL_ICON, text: DEL_BOTTON_LABEL, handler: this.doDel, scope: this}));
    }   
   
    ,showContextMenu : function(){           
        this.setContextMenu();
        this.on('rowcontextmenu', this.onGridRangesContextMenu, this);
    }
    
    ,onGridRangesContextMenu : function(grid, rowIndex, e) { 
          e.stopEvent(); 
          this.contextMenu.showAt(e.getXY());
    }  

});














Ext.ux.CascadeComboBox = function(config){
    Ext.ux.CascadeComboBox.superclass.constructor.call(this, config);
    Ext.apply(this,config); 
    this.valueField = 'id';
    this.displayField = 'text';
    this.store = new Ext.ux.FilterStore({codeObjectName:this.codeObjectName}); 
    this.tree = null;
    this.treeWin = null;
    this.lastValue = null;
    this.nowValue = null;
    this.on("valid",function(){
      this.nowValue = this.hiddenField.value;
      if(this.nowValue!==0&&this.nowValue!==null&&this.nowValue!==''&&this.nowValue!==this.lastValue){
          //alert("store load: this.nowValue="+this.nowValue+" this.lastValue="+this.lastValue);
          this.lastValue = this.nowValue;
          this.store.on("load",function(){              
              //this.hiddenField.value = this.value;
              this.setValue(this.nowValue);
              //alert("load end: this.nowValue="+this.nowValue+" this.lastValue="+this.lastValue);
          },this,{single: true}); 
          //this.store.load({params:{cascadeComboValue:this.nowValue,add:true}}); 
          this.store.load({params:{cascadeComboValue:this.nowValue}});        
      }
    },this);
//    this.on("valid",function(){
//      this.nowValue = this.getValue();
//      if(this.nowValue!==0&&this.nowValue!==null&&this.nowValue!==''){
//          this.setValue("value:index");
//          //this.setValue('vvvvv':'iiiiiii');
//      }
//    },this);
//    this.loaded = false;

//    this.params = {codeJson:"{'codeObjectName':'"+this.codeObjectName+"'}"};
//    this.on("focus",function(){
//                             if(this.getValue()==0||this.getValue()==null||this.getValue()==''){
//                                if(!this.loaded){
//                                  this.store.load({params:this.params});
//                                  this.loaded = true
//                                }
//                             }
//                          },this);
//    this.on("valid",function(){
//                             if(this.getValue()!==0&&this.getValue()!==null&&this.getValue()!==''){
//                                if(!this.loaded){
//                                  this.load();
//                                  this.loaded = true
//                                }
//                             }
//                          },this);
};
Ext.extend(Ext.ux.CascadeComboBox, Ext.form.ComboBox, {  
     typeAhead: false
    ,resizable: true
    //,hideTrigger:true
    ,minChars:2
    //,triggerAction: 'all'
    ,emptyText:'至少输入2个字才能触发'
    //,selectOnFocus:true
    //,lazyInit : true    
    
    ,load : function(){
        this.store.on("load",function(){this.setValue(this.getValue())},this,{single: true});
        this.store.load({params:this.params});
    }
    
    ,trigger2Class:'x-form-search-trigger'
    
    
    ,initComponent : function(){
        Ext.ux.CascadeComboBox.superclass.initComponent.call(this);

        this.triggerConfig = {
            tag:'span', cls:'x-form-twin-triggers', cn:[
            {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger1Class},
            {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger2Class}
        ]};
    }    
    
    
    ,initTrigger : function(){
        var ts = this.trigger.select('.x-form-trigger', true);
        this.wrap.setStyle('overflow', 'hidden');
        var triggerField = this;
        ts.each(function(t, all, index){
            t.hide = function(){
                var w = triggerField.wrap.getWidth();
                this.dom.style.display = 'none';
                triggerField.el.setWidth(w-triggerField.trigger.getWidth());
            };
            t.show = function(){
                var w = triggerField.wrap.getWidth();
                this.dom.style.display = '';
                triggerField.el.setWidth(w-triggerField.trigger.getWidth());
            };
            var triggerIndex = 'Trigger'+(index+1);

            if(this['hide'+triggerIndex]){
                t.dom.style.display = 'none';
            }
            
            if(index==0){
               t.on("click", this.onTriggerClick, this, {preventDefault:true});
            }else{
               t.on("click", this['on'+triggerIndex+'Click'], this, {preventDefault:true});
            }
            
            t.addClassOnOver('x-form-trigger-over');
            t.addClassOnClick('x-form-trigger-click');
        }, this);
        this.triggers = ts.elements;
    }
    
    ,setValueAndCloseWin : function(){
        var selectedModel = this.tree.getSelectionModel();
        var selectedNode = selectedModel.getSelectedNode();
        if(selectedNode.isLeaf()){
            //alert("isLeaf " + selectedNode.id+ " "+ selectedNode.text);
            //this.setValue(selectedNode.id);
            //this.setValue(selectedNode.text);
            //this.doQuery(this.getRawValue());
            
            //this.setValue(selectedNode.text);
            
            //this.onTriggerClick();
            
            
            var id = selectedNode.id;
            var ids = id.split(this.tree.pathSeparator);
            id = ids[ids.length - 1];
            this.setValue(id);
            
            //this.store.on('load',function(){this.setValue(id);this.collapse();},this,{single: true});
            
            
            
            
            //this.doQuery(selectedNode.text);
            //this.setValue(selectedNode.id);
            //this.doQuery(this.allQuery, true);
            //this.on('');
            this.treeWin.hide();
            //
        }
    }
    
    ,onTrigger1Click : Ext.emptyFn
    ,onTrigger2Click : function(){
       if(!this.treeWin){
        this.tree = new Ext.tree.TreePanel({
                //el:'tree',
                //id:'im-tree',//注意，如果要做成通用的树，怎么能用id呢
                rootVisible:false,
                //selModel: new Ext.tree.MultiSelectionModel,//可以多选
                selModel: new Ext.tree.DefaultSelectionModel,
                //renderTo: document.body,
                //hideBorders:true, 
                width : 600,
                height : 600,
                animate:true, 
                autoScroll:true,
                loader: new Ext.tree.TreeLoader({dataUrl:'/MISFT/CodeJson.do'}),
                containerScroll: true,
                dropConfig: {appendOnly:true}
            });
            new Ext.tree.TreeSorter(this.tree, {folderSort:true});
            var root = new Ext.tree.AsyncTreeNode({
                text: '组织机构', 
                draggable:false, // disable root node dragging
                id:this.codeObjectName
            });
            this.tree.setRootNode(root);
            //this.tree.render();            
            root.expand(false, /*no anim*/ false); 
        
	    //this.tree.getSelectionModel().on('beforeselect',this.setValueAndCloseWin);
	    this.tree.getSelectionModel().on('selectionchange',this.setValueAndCloseWin,this);
        
        
        this.treeWin = new Ext.Window({
                layout:'fit',
                collapsible:true,
                modal:true,//make the window modal and mask everything behind it when displayed
                minimizable:true,
                maximizable:true,
                constrainHeader:true,
                title:"请选择",
                width:200,
                height:300,
                closeAction:'hide',
                //plain: true,
                autoDestroy : true,                
                items: this.tree                
            });
       }
       this.treeWin.show();
        
//        Ext.select('a span').on('click', function(){
//	                  alert("kao");
//	                  alert("kao");
//	    });
    }
//    ,onSelect: function(record){ // override default onSelect to do redirect
//            alert("dd");
//    }
    
});
Ext.reg('ux.cascadecombo', Ext.ux.CascadeComboBox);//实现自动完成和树型选择














Ext.ux.grid.myGridFilters = Ext.extend(Ext.ux.grid.GridFilters, {
         buildQuery: function(filters){
						var p = {};
						var filterJson = Ext.util.JSON.encode(filters);
						p['filterJson']=filterJson;
						return p;
		 }
});












